<template>
  <div class='app-container'>
    <el-form v-show='showSearch' ref='queryForm' :inline='true' :model='queryParams'
             @keyup.enter.native='getDataList()'>
      <el-form-item>
        <el-button
          v-has-permission="['sys:config:add']"
          icon='el-icon-plus'
          plain
          size='mini'
          type='primary'
          @click='handleAdd'
        >新增
        </el-button>
        <el-button
          v-has-permission="['sys:config:delete']"
          :disabled='multiple'
          icon='el-icon-delete'
          plain
          size='mini'
          type='danger'
          @click='handleDelete'
        >批量删除
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-input v-model='queryParams.paramKey' clearable placeholder='参数名' />
      </el-form-item>
      <el-form-item>
        <el-button icon='el-icon-search' size='mini' type='primary' @click='handleQuery'>搜索</el-button>
        <el-button icon='el-icon-refresh' size='mini' @click='handleReset'>重置</el-button>
        <!--        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
        <!--        <el-button type="danger" :disabled="dataListSelections.length <= 0" @click="deleteHandle()">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      v-loading='loading'
      :data='dataList'
      border
      style='width: 100%;'
      @selection-change='handleSelectionChange'
    >
      <el-table-column
        align='center'
        header-align='center'
        type='selection'
        width='50'
      />
      <el-table-column
        align='center'
        header-align='center'
        label='ID'
        prop='id'
        width='80'
      />
      <el-table-column
        align='center'
        header-align='center'
        label='参数名'
        prop='paramKey'
      />
      <el-table-column
        align='center'
        header-align='center'
        label='参数值'
        prop='paramValue'
      />
      <el-table-column
        align='center'
        header-align='center'
        label='备注'
        prop='remark'
      />
      <el-table-column
        align='center'
        fixed='right'
        header-align='center'
        label='操作'
        width='150'
      >
        <template slot-scope='scope'>
          <el-button size='small' type='text' @click='handleUpdate(scope.row.id)'>修改</el-button>
          <el-button size='small' type='text' @click='handleDelete(scope.row.id)'>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show='pagination.total > 0'
      :limit.sync='pagination.pageSize'
      :page.sync='pagination.pageNum'
      :total='pagination.total'
      @pagination='handleQuery'
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if='addOrUpdateVisible' ref='addOrUpdate' @refreshDataList='getDataList' />
  </div>
</template>

<script>
import AddOrUpdate from './config-add-or-update'
import { del, list } from '@/api/admin/sysConfig'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        paramKey: undefined
      },
      dataList: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      addOrUpdateVisible: false
    }
  },
  async created() {
    this.getDataList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.loading = true
      Object.keys(this.queryParams).forEach(key => {
        this.queryParams[key] === '' ? this.queryParams[key] = null : false
      })
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      list(this.queryParams).then(response => {
        const { data } = response
        if (data) {
          this.dataList = data.list
          this.pagination.total = data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.loading = false
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 新增
    handleAdd() {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init()
      })
    },
    // 修改
    handleUpdate(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    handleDelete(id) {
      const ids = id ? [id] : this.ids
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del.then(({ data }) => {
          if (data && data.code === 20000) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.message)
          }
        })
      }).catch(() => {
      })
    },
    handleReset() {
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.resetQuery()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    resetForm(val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    }
  }
}
</script>
